var ctx;
var page = 1;
var search = "";
var id = 0;

function setCtx(ctx){
	this.ctx = ctx;
}

var listSize;
var len = $("#listSize").val();

var isAddDict = false;

//新增或编辑页面点击添加按钮追加一条数据
function addDict(){
/*	if(removeDict){
		len = --len
	}*/
	len = ++len;
	var index = len-1;
	$("#aedict").append('<div id="aedictId' +index + '"class="item">'+
			'<label class="col-sm-1 editlable">数据'+ len +'：</label>'+
			'<div class="col-sm-2 editdiv" style="margin-left:15px;margin-top:3px;">'+
				'<input id="label' +index + '" type="text" maxlength="50" value="" class="form-control1" placeholder="标签名" required>'+
			'</div>'+
			'<div class="col-sm-2 editdiv" style="margin-left:10px;margin-top:3px;">'+
				'<input  id="value'+index + '" type="number" value="" maxlength="1" class="form-control1" placeholder="数据值" required>'+
				'<span id="checkvalue" style="display: none;color: red;">数据值不能为空</span>'+
			'</div>'+
			'<div class="col-sm-2 editdiv" style="margin-left:10px;margin-top:3px;">'+
				'<input id="sort'+index + '" value="" type="number" class="form-control1 sort" placeholder="顺序" max="25" min="1">'+
			'</div>'+
			'<div class="col-sm-2" style="margin-left:5px;">'+
				'<a class="search" onclick="addDict();" style="float:left; margin-top:3px; padding: 0px 5px 0px 5px;"><img width="80%" src="/manage/images/table/add.png"></a>'+
				'<a class="search" onclick="deleteDict('+index+');" style="float:left;margin-top:3px;"><img width="80%" src="/manage/images/table/delete.png"></a>'+
			'</div></div>');
	isAddDict = true;
	initPlaceHolder();
}

//将当前选中数据移除，此时dictVos集合中当前记录为空
//var removeDict = false;
function deleteDict(did){
	$("#aedictId" + did).remove();
}

//验证描述不能为空
function checkDescript(){
	 var description = $("#description").val();
	//解决ie9以下placeholder与value重复的问题
		if(description == $("#description").attr("placeholder")){
			description = "";
		}
		if(description == null || description == ""){
			layer.tips('描述不能为空!', '#description', {
				tips: 3,
			    time:1000
			});
			return false;
		}else{
			return true;
	}
}

//验证类型必须为英文
function checkType(){
	var type = $("#type").val();
	//解决ie9以下placeholder与value重复的问题
	if(type == $("#type").attr("placeholder")){
		type = "";
	}
	if(type == null || type == ""){
		layer.tips('字典类型不能为空!', '#type', {
			tips: 3,
		    time:1000
		});
		return false;
	}else{
		var reg= /^[A-Za-z]+$/;
		if(!reg.test(type)){
			layer.tips('字典类型必须为英文!', '#type', {
				tips: 3,
			    time:1000
			});
			return false;
		}else{
			return true;
		}
	}
}

var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
function saveOrUpdateDict(){
	if(isAddDict){
		listSize = len;
	}else{
		listSize = $("#listSize").val();
	}
	var list = [];
	for(var i=0;i < listSize;i++){
		var dictVo = {};
		dictVo.label = $("#label" + i).val();
		dictVo.dictId = Number($("#dictId" + i).val());
		dictVo.value = Number($("#value" + i).val());
		dictVo.sort = Number($("#sort" + i).val());
		/* list.push("{'dictId':"+$("#dictId" + i).val()+",'label':'"+$("#label" + i).val()+"','value':"+$("#value" + i).val()+",'sort':"+$("#sort" + i).val()+"}") */
		if(dictVo.label==null || dictVo.value==null || dictVo.sort==null){
			continue;
		}
		list.push(dictVo);
	}
	var descript = $("#description").val();
	var type = $("#type").val();
	var dictid = $("#dictid").val();

	if(checkDescript() && checkType()){
		var index1 = layer.load(1);
		
		$.post(ctx+"/dict/updateDict",{
			dictVos:JSON.stringify(list),
			descript:descript,
			type:type,
			dictid:dictid
		},function(data){
			data = JSON.parse(data);
			if(data.success){
				layer.msg("操作成功",{
					icon: 1,
					time: 2000 //2秒关闭（如果不配置，默认是3秒）
				},function(){
					parent.loadContent("/dict/dictList");
					parent.layer.close(index);
				});
			} else {
				layer.close(index1);
				layer.msg(data.msg,{
					icon: 2,
					time: 2000 //2秒关闭（如果不配置，默认是3秒）
				});
			}
		});
	}
}
